package com.jalarbee.web.jsf.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;
import org.apache.commons.beanutils.BeanUtils;


public class CustomJsfUtil {

	public static List<SelectItem> toSelectItemsList(List<?> objects, String key, String label) {
		List<SelectItem> items = new ArrayList<SelectItem>();
		SelectItem item = null;
		String id = null;
		String desc = null;
		for(Object object : objects) {
			try {
				id = BeanUtils.getProperty(object, key);
				desc = BeanUtils.getProperty(object, label);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			}
			item = new SelectItem(id, desc);
			items.add(item);
		}
		return items;
	}
}
